package com.hotel.controller;

import com.hotel.pojo.Customer;
import com.hotel.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author shkstart 客户管理后台控制器
 * @create 2021-06-26 19:10
 */
@Controller
public class CustomerController {

    @Autowired(required = false)
    private CustomerService customerService;

    //跳转到客户管理界面
    @RequestMapping("/skip/toListCustomer")
    public String toListCustomer(Model model) {
        List<Customer> all = customerService.list(null);
        model.addAttribute("customer",all);
        return "admin/views/listCustomer";
    }


    //跳转到修改客户信息界面
    @RequestMapping("/toUpdateCustomer/{id}")
    public String UpdateCustomer(@PathVariable("id")Integer id, Model model,Customer customer){
        Customer byId = customerService.getById(id);
        model.addAttribute("byId",byId);
        return "admin/views/update/CustomerUpdate";
    }

    //修改用户
    @RequestMapping("/toUpdateCs")
    public String toUpdate(Customer customer){
        boolean b = customerService.updateById(customer);
        System.out.println("客户信息修改的结果为===>" + b);
        return "redirect:/skip/toListCustomer";
    }

    //跳转到用户添加界面
    @RequestMapping("/skip/toCustomerAdd")
    public String toAddCustomer(){
        return "admin/views/add/CustomerAdd";
    }

    //添加用户
    @RequestMapping("/toAddCustomer")
    public String addCustomer(Customer customer){
        String s = customer.toString();
        System.out.println(s);
        boolean save = customerService.save(customer);
        System.out.println("用户添加信息的结果为====>" + save);
        return "redirect:/skip/toListCustomer";
    }

    //删除客户
    @RequestMapping("/deletedCustomer/{id}")
    public String deleted(@PathVariable("id") int id){
        boolean b = customerService.removeById(id);
        System.out.println("客户信息删除的结果为====>" + b);
        return "redirect:/skip/toListCustomer";
    }


}
